var report={};
report.Log={};
report.Log.initGrid = function(){
	var options = {},
    colNames = ["序号","名称","描述","类别","添加时间","添加人"];
    colModel = [
    {name:"id",index:"id",width:10,key:true},
    {name:"name",index:"name",width:35},
    {name:"description",index:"description",width:55},
    {name:"type",index:"type",width:25},
    {name:"addtime",index:"addtime",width:25},
    {name:"inputer",index:"inputer",width:30}
];
options = {colNames:colNames,
           colModel:colModel,
           newUrl:"preAdd.jspx",
           editUrl:"preEdit.jspx?id=",
           delUrl:"del.jspx?id=",
           canEdit:false,
           canAdd:false,
           canDel:true,
           canSearch:false,
           caption:"日志列表"};

customGrid.init(options);
};

report.ProductStock={};
report.ProductStock.initGrid = function(){
	var options = {},
    colNames = ["序号","材料编号","材料名称","库存","最后库存","单位","年","月","日"];
    colModel = [
    {name:"id",index:"id",width:10,key:true},
    {name:"materialCode",index:"materialCode",width:35}
    ,{name:"materialName",index:"materialName",width:35}
    ,{name:"stock",index:"stock",width:25},
    {name:"lastStock",index:"lastStock",width:25},
    {name:"unit",index:"unit",width:8},
    {name:"year",index:"year",width:25},
    {name:"month",index:"month",width:30}
    ,{name:"day",index:"day",width:30}
    ];
    options = {colNames:colNames,
           colModel:colModel,
           canEdit:false,
           canAdd:false,
           canDel:false,
           caption:"产品库存列表"};

    customGrid.init(options);
};

report.MaterialStock={};
report.MaterialStock.initGrid = function(){
	var options = {},
    colNames = ["序号","材料编号","材料名称","库存","最后库存","单位","年","月","日"];
    colModel = [
    {name:"id",index:"id",width:35,fixed:true,key:true},
    {name:"materialCode",index:"materialCode",width:15},
    {name:"materialName",index:"materialName",width:35},
    {name:"stock",index:"stock",width:15},
    {name:"lastStock",index:"lastStock",width:15},
    {name:"unit",index:"unit",width:8},
    {name:"year",index:"year",width:8},
    {name:"month",index:"month",width:8}
    ,{name:"day",index:"day",width:8}
    ];
    options = {colNames:colNames,
           colModel:colModel,
           jsonUrl:"getJson.jspx?format=json&code=0"
           ,canEdit:false
           ,canAdd:false
           ,canDel:false
           ,caption:"材料库存列表"};

    customGrid.init(options);
};
report.MaterialStock.init = function(){
	$.get("getInfo.jspx?format=json",function(data){
		if(data!==undefined){
			if(data.currentYear!==undefined && data.currentYear>0){
				$("#report_year").val(data.currentYear);
			}
			if(data.currentMonth!==undefined && data.currentMonth>0){
				$("#report_month").val(data.currentMonth);
			}
			if(data.day!==undefined){
				$("#report_day").empty();
				for(var i=1;i<=data.day;i++){
					$("#report_day").append("<option value='"+i+"'>"+i+"</option>");
				}
			}
			if(data.currentDay!==undefined && data.currentDay>0){
				$("#report_day").val(data.currentDay);
			}
			
			if(data.categories!==undefined){
				var $report_code = $("#report_code");
				$report_code.empty();
				$report_code.append('<option value="0">全部</option>');
				$.each($(data.categories),function(index,category){
					$report_code.append('<option value="'+category.code+'">'+category.name+'</option>');
				});
			}
		}
	});
	
	$("#btn_year").on("click",function(){
		report.MaterialStock.year();
	});
	
	$("#btn_month").on("click",function(){
		report.MaterialStock.month();
	});
	
	$("#btn_day").on("click",function(){
		report.MaterialStock.day();
	});
	
	$("#btn_excel").on("click",function(){
		report.MaterialStock.excel();
	});
};
report.MaterialStock.year = function(){
	var $year = $("#report_year").val()
	    , $code = $("#report_code").val()
	    , $codeText = $("#report_code option:selected").text()
	    , $caption = $year+"年"+$codeText+"库存报表";
	var options = {};
    options = {url:"getJson.jspx?format=json&code="+$code+"&year="+$year+"&action=year&caption="+$caption
			,name:$caption};
	customGrid.changeCaption(options);
    customGrid.hideCol('month');
    customGrid.hideCol('day');
    customGrid.changeWidth($('body').width());
	customGrid.reload(options);
    
};
report.MaterialStock.month = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $code = $("#report_code").val()
	  , $codeText = $("#report_code option:selected").text()
	  , $caption = $year+"年"+$month+"月"+$codeText+"库存报表"
	  , options = {};
    options = {url:"getJson.jspx?format=json&code="+$code+"&action=month&year="+$year+"&month="+$month+"&caption="+$caption
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.showCol('month');
    customGrid.hideCol('day');
    customGrid.changeWidth($('body').width());
    customGrid.reload(options);
    
};
report.MaterialStock.day = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $day = $("#report_day").val()
	  , $code = $("#report_code").val()
	  , $codeText = $("#report_code option:selected").text()
	  , $caption = $year+"年"+$month+"月"+$day+"日"+$codeText+"库存报表"
	  , options = {};
    options = {
           url:"getJson.jspx?format=json&code="+$code+"&action=day&year="+$year+"&month="+$month+"&day="+$day+"&caption="+$caption
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.showCol('month');
    customGrid.showCol('day');
    customGrid.changeWidth($('body').width());
	customGrid.reload(options);
    
};
report.MaterialStock.excel = function(){
	var $excelResult = $("#excel_result");
	$excelResult.html('<span style="color:green">生成中...</span>');
	var url = $("#gridTable").jqGrid('getGridParam','url');
	console.debug('url:'+url);
	url = url.replace("getJson","exportExcel");
	$.get(url+"&pageable=false",function(data){
		var result = data.result;
		if(result===true){
			var filename = data.filename;
			$excelResult.html('<a href="download.jspx?format=json&filename='+filename+'&sheetname='+data.sheetname+'" title="'+data.sheetname+'">下载报表</a>');
		}else{
			$excelResult.html('<span style="color:red">'+data.message+'</span>');
		}
	});
};

report.Task={};
report.Task.initGrid = function(){
	var options = {}
    , colNames = ["序号","计划编号","任务单","规格型号","颜色","生产数量","单位","工位","生产人员","开始日期","完成日期","用时","状态"]
    , colModel = [
    {name:"id",index:"id",width:10,key:true,align:"center"},
    {name:"planCode",index:"planCode",width:25},
    {name:"taskCode",index:"taskCode",width:15,sortable:false},
    {name:"materialModel",index:"materialModel",width:35},
    {name:"materialDesc",index:"materialDesc",width:15},
    {name:"needTotal",index:"needTotal",width:15},
    {name:"unit",index:"unit",width:8},
    {name:"stationName",index:"stationId",width:20},
    {name:"realname",index:"realname",width:20,sortable:false},
    {name:"createDate",index:"createDate",width:20},
    {name:"finishDate",index:"finishDate",width:20},
    {name:"countTime",index:"countTime",width:15,sortable:false},
    {name:"statusMessage",index:"status",width:15}
    ];
    options = {colNames:colNames,
           colModel:colModel,
           jsonUrl:"getJson.jspx?format=json"
           ,canEdit:false
           ,canAdd:false
           ,canDel:false
           ,canSearch:false
           ,caption:"任务分配列表"};

    customGrid.init(options);
};
report.Task.init = function(){
	$.get("getInfo.jspx?format=json",function(data){
		if(data!==undefined){
			if(data.currentYear!==undefined && data.currentYear>0){
				
				
				var $year = $('#report_year');
				$year.empty();
				$year.append('<option value="0">全部</option>')
				for(var year=2005;year<=data.currentYear;year++){
					$year.append('<option value="'+year+'">'+year+'</option>');
				}
				
				$("#report_year").val(data.currentYear);
			}
			if(data.currentMonth!==undefined && data.currentMonth>0){
				$("#report_month").val(data.currentMonth);
			}
			if(data.day!==undefined){
				var $reportDay = $("#report_day");
				$reportDay.empty();
				for(var i=1;i<=data.day;i++){
					$reportDay.append("<option value='"+i+"'>"+i+"</option>");
				}
			}
			if(data.currentDay!==undefined && data.currentDay>0){
				$("#report_day").val(data.currentDay);
			}
			
			if(data.stations!==undefined && data.stations.length>0){
				var $station = $('#report_station');
				$station.empty();
				$station.append('<option value="0">全部</option>');
				$.each(data.stations,function(index,station){
					$station.append('<option value="'+station.id+'">'+station.name+'</option>');
				});
			}
			
			$('#report_station').on('change',function(){
				var stationId = $(this).val();
				var $user = $('#report_user');
				$user.empty();
				$user.append('<option value="0">全部</option>');
				$.get('getUser.jspx?format=json',{stationId:stationId},function(data){
					if(data!==undefined && data.users!==undefined){
						$.each(data.users,function(index,user){
							$user.append('<option value="'+user.id+'">'+user.username+'('+user.realname+')');
						});
					}
				});
			});
			
			$('#report_from_date').val(data.monday);
			$('#report_end_date').val(data.today);
			
			var dayNames=["周日","周一","周二","周三","周四","周五","周六"];
			var monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];

			$('.datepicker').datepicker({
				dateFormat:"yy-mm-dd",
				autoSize:true,
				dayNamesMin:dayNames,
				monthNames:monthNames
			});
		}
	});
	
	$("#btn_year").on("click",function(){
		report.Task.year();
	});
	
	$("#btn_month").on("click",function(){
		report.Task.month();
	});
	
	$('#btn_weekly').on('click',function(){
		report.Task.weekly();
	});
	
	$("#btn_day").on("click",function(){
		report.Task.day();
	});
	
	$("#btn_excel").on("click",function(){
		report.excel();
	});
};
report.Task.year = function(){
	var $year = $("#report_year").val()
	    , $caption = $year+"年任务分配报表",
	    stationId = $('#report_station').val(),
	    userId = $('#report_user').val();
	var options = {};
    options = {url:"getJson.jspx?format=json&year="+$year+"&stationId="+stationId+"&userId="+userId+"&action=year&caption="+$caption
			,name:$caption};
	customGrid.changeCaption(options);
    customGrid.changeWidth($('body').width());
	customGrid.reload(options);
    
};
report.Task.month = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $caption = $year+"年"+$month+"月任务分配报表"
	  , options = {},
	    stationId = $('#report_station').val(),
	    userId = $('#report_user').val();
    options = {url:"getJson.jspx?format=json&action=month&year="+$year+"&month="+$month+"&stationId="+stationId+"&userId="+userId+"&caption="+$caption
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.changeWidth($('body').width());
    customGrid.reload(options);
    
};
report.Task.weekly = function(){
	var fromDate = $('#report_from_date').val(),
	    endDate = $('#report_end_date').val(),
	    caption = fromDate+" 至 "+endDate+" 任务分配报表",
	    stationId = $('#report_station').val(),
	    userId = $('#report_user').val(),
	    url = "getJson.jspx?format=json&action=weekly&fromDate="+fromDate+"&endDate="+endDate+"&stationId="+stationId+"&userId="+userId+"&caption="+caption,
	    options = {url:url,name:caption};
	customGrid.changeCaption(options);
	customGrid.changeWidth($('body').width());
	customGrid.reload(options);
};
report.Task.day = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $day = $("#report_day").val()
	  , $caption = $year+"年"+$month+"月"+$day+"日任务分配报表"
	  , options = {},
	    stationId = $('#report_station').val(),
	    userId = $('#report_user').val();
    options = {
           url:"getJson.jspx?format=json&action=day&year="+$year+"&month="+$month+"&day="+$day+"&stationId="+stationId+"&userId="+userId+"&caption="+$caption
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.showCol('month');
    customGrid.showCol('day');
    customGrid.changeWidth($('body').width());
	customGrid.reload(options);
    
};
report.excel = function(){
	var $excelResult = $("#excel_result");
	$excelResult.html('<span style="color:green">生成中...</span>');
	var url = $("#gridTable").jqGrid('getGridParam','url');
	console.debug('url:'+url);
	url = url.replace("getJson","exportExcel");
	$.get(url+"&pageable=false",function(data){
		var result = data.result;
		if(result===true){
			var filename = data.filename;
			$excelResult.html('<a href="download.jspx?filename='+filename+'&sheetname='+data.sheetname+'" title="'+data.sheetname+'">下载报表</a>');
		}else{
			$excelResult.html('<span style="color:red">'+data.message+'</span>');
		}
	});
};

report.StockOut={};
report.StockOut.initGrid = function(){
	var options = {}
    , colNames = ["序号","工位","用户名","物料代号","物料名称","规格型号","申请数量","最终数量","单位","领取时间"]
    , colModel = [
    {name:"id",index:"id",width:45,fixed:true,key:true,align:"center"},
    {name:"stationName",index:"stationName",width:8},
    {name:"userName",index:"userName",width:8},
    {name:"materialCode",index:"materialCode",width:10},
    {name:"materialName",index:"materialName",width:12},
    {name:"model",index:"model",width:15}
    ,{name:"needNum",index:"needNum",width:6}
    ,{name:"finalNum",index:"finalNum",width:6}
    ,{name:"unit",index:"unit",width:5,align:"center"}
    ,{name:"edittime",index:"edittime",width:10}
    ];
    options = {colNames:colNames,
           colModel:colModel,
           jsonUrl:"getJson.jspx?format=json"
           ,canEdit:false
           ,canAdd:false
           ,canDel:false
           ,caption:"工位物料领取列表"};

    customGrid.init(options);
};
report.StockOut.init = function(){
	$.get("getInfo.jspx?format=json",function(data){
		if(data!==undefined){
			if(data.stations!==undefined){
				$station = $("#report_stationid");
				$station.empty();
				$station.append('<option value="0">全部</option>');
				$.each(data.stations,function(index,station){
					$station.append('<option value="'+station.id+'">'+station.name+'</option>');
				});
			}
			if(data.currentYear!==undefined && data.currentYear>0){
				var $report_year = $("#report_year");
				$report_year.empty();
				for(var i=2005;i<=data.currentYear;i++){
					$report_year.append('<option value="'+i+'">'+i+'</option>');
				}
				$report_year.val(data.currentYear);
			}
			if(data.currentMonth!==undefined && data.currentMonth>0){
				$("#report_month").val(data.currentMonth);
			}
			if(data.day!==undefined){
				$("#report_day").empty();
				for(var i=1;i<=data.day;i++){
					$("#report_day").append("<option value='"+i+"'>"+i+"</option>");
				}
			}
			if(data.currentDay!==undefined && data.currentDay>0){
				$("#report_day").val(data.currentDay);
			}
		}
	});
	
	$("#btn_year").on("click",function(){
		report.StockOut.year();
	});
	
	$("#btn_month").on("click",function(){
		report.StockOut.month();
	});
	
	$("#btn_day").on("click",function(){
		report.StockOut.day();
	});
	
	$("#btn_excel").on("click",function(){
		report.excel();
	});
};
report.StockOut.year = function(){
	var $year = $("#report_year").val()
		, $stationId = $("#report_stationid").val()
		, $stationText = $("#report_stationid option:selected").text()
	    , options = {};
	var $caption = $year+"年"+$stationText+"工位物料领取报表";
	if($year==0){
		alert("请选择年份");
		return;
	}
	    options = {url:"getJson.jspx?format=json&year="+$year+"&action=year&caption="+$caption+"&stationId="+$stationId
				,name:$caption};
		customGrid.changeCaption(options);
	    customGrid.changeWidth($('body').width());
		customGrid.reload(options);
};
report.StockOut.month = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $stationId = $("#report_stationid").val()
	  , $stationText = $("#report_stationid option:selected").text()
	  , $caption = $year+"年"+$month+"月"+$stationText+"工位物料领取报表"
	  , options = {};
	if($year==0 || $month==0){
		alert("请选择年份或月份");
		return;
	}
    options = {url:"getJson.jspx?format=json&action=month&year="+$year+"&month="+$month+"&caption="+$caption+"&stationId="+$stationId
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.changeWidth($('body').width());
    customGrid.reload(options);
    
};
report.StockOut.day = function(){
	var $year = $("#report_year").val()
	  , $month = $("#report_month").val()
	  , $day = $("#report_day").val()
	  , $stationId = $("#report_stationid").val()
	  , $stationText = $("#report_stationid option:selected").text()
	  , $caption = $year+"年"+$month+"月"+$day+"日"+$stationText+"工位物料领取报表"
	  , options = {};
	if($year==0 || $month==0 || $day==0){
		alert("请选择年月日信息");
		return;
	}
    options = {
           url:"getJson.jspx?format=json&action=day&year="+$year+"&month="+$month+"&day="+$day+"&caption="+$caption+"&stationId="+$stationId
           ,name:$caption};

	customGrid.changeCaption(options);
    customGrid.showCol('month');
    customGrid.showCol('day');
    customGrid.changeWidth($('body').width());
	customGrid.reload(options);
    
};
